package com.koron.order.repair.service.impl;

import com.koron.common.core.util.CodeTools;
import com.koron.order.repair.bean.entity.RepairEquipmentPo;
import com.koron.order.repair.bean.vo.RepairEquipmentVo;
import com.koron.order.repair.bean.vo.RepairItemVo;
import com.koron.order.repair.mapper.RepairEquipmentMapper;
import com.koron.order.repair.service.RepairEquipmentService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * (RepairEquipment)表服务实现类
 *
 * @author zhouj
 * @since 2021-09-14 14:25:19
 */
@Service("repairEquipmentService")
public class RepairEquipmentServiceImpl implements RepairEquipmentService {

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, RepairEquipmentPo repairEquipment) {

        RepairEquipmentMapper mapper = factory.getMapper(RepairEquipmentMapper.class);

        return mapper.insert(repairEquipment);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, RepairEquipmentPo repairEquipment) {
        RepairEquipmentMapper mapper = factory.getMapper(RepairEquipmentMapper.class);
        return mapper.update(repairEquipment);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        RepairEquipmentMapper mapper = factory.getMapper(RepairEquipmentMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("deleteByOrderId")
    public int deleteByOrderId(SessionFactory factory, String orderId) {

        RepairEquipmentMapper mapper = factory.getMapper(RepairEquipmentMapper.class);
        return mapper.deleteByOrderId(orderId);
    }

    @Override
    @TaskAnnotation("queryById")
    public RepairEquipmentPo queryById(SessionFactory factory, String id) {

        RepairEquipmentMapper mapper = factory.getMapper(RepairEquipmentMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryByOrderId")
    public List<RepairEquipmentVo> queryByOrderId(SessionFactory factory, String orderId) {

        RepairEquipmentMapper mapper = factory.getMapper(RepairEquipmentMapper.class);

        List<RepairEquipmentVo> list = mapper.queryByOrderId(orderId);

        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }

        for (RepairEquipmentVo repairEquipmentVo : list) {
            List<RepairItemVo> itemVos = repairEquipmentVo.getItemVos();
            if (CollectionUtils.isEmpty(itemVos)) {
                continue;
            }
            Map<String, RepairItemVo> map = new HashMap<>();
            for (RepairItemVo itemVo : itemVos) {
                String id = itemVo.getId();
                String parentId = itemVo.getParentId();
                itemVo.setRepairItemVoList(new ArrayList<>());
                if (StringUtils.isEmpty(parentId)) {
                    map.put(id, itemVo);
                }
            }

            Iterator<RepairItemVo> iterator = itemVos.iterator();

            while (iterator.hasNext()) {
                RepairItemVo itemVo = iterator.next();
                String id = itemVo.getId();
                String parentId = itemVo.getParentId();
                if (StringUtils.isNotEmpty(parentId)) {
                    RepairItemVo repairItemVo = map.get(parentId);
                    if (!Objects.isNull(repairItemVo)) {
                        //找到了父节点,应该从原来列表删除
                        iterator.remove();
                        List<RepairItemVo> repairItemVoList = repairItemVo.getRepairItemVoList();
                        repairItemVoList.add(itemVo);
                    }
                }
            }
        }

        return list;
    }

    @Override
    @TaskAnnotation("queryList")
    public List<RepairEquipmentPo> queryList(SessionFactory factory, RepairEquipmentPo repairEquipment) {

        RepairEquipmentMapper mapper = factory.getMapper(RepairEquipmentMapper.class);

        List<RepairEquipmentPo> list = mapper.queryList(repairEquipment);

        return list;
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<RepairEquipmentPo> repairEquipmentPoList) {
        if (CollectionUtils.isEmpty(repairEquipmentPoList)) {
            return 0;
        }
        String repairId = repairEquipmentPoList.get(0).getRepairId();
        deleteByOrderId(factory, repairId);

        RepairEquipmentMapper mapper = factory.getMapper(RepairEquipmentMapper.class);
        repairEquipmentPoList.forEach(p -> {
            if (StringUtils.isEmpty(p.getId())) {
                p.setId(CodeTools.getCode32());
            }
        });

        return mapper.insertBatch(repairEquipmentPoList);
    }

}